package com.jeeplus.test.jlnu.admissionInfo.controller;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/pdf")
public class PdfSignatureController {


    @PostMapping("/sign")
    public void signPdf(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        try {
            // 读取 PDF
            PDDocument document = PDDocument.load(file.getInputStream());
            PDPage page = document.getPage(0);
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

            // 生成红色圆形签章
            float width = page.getMediaBox().getWidth();
            float radius = 50; // 签章半径
            float centerX = width / 2;
            float centerY = 80; // 底部距离

            contentStream.setStrokingColor(Color.RED);
            contentStream.setLineWidth(2);

            // 画圆形（贝塞尔曲线）
            drawCircle(contentStream, centerX, centerY, radius);
            contentStream.stroke();

            // 加载支持中文的字体
            File fontFile = new File("C:/Windows/Fonts/simhei.ttf"); // 你可以换成其他支持中文的 TTF 字体
            PDType0Font font = PDType0Font.load(document, fontFile);

            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.setNonStrokingColor(Color.RED);
            contentStream.newLineAtOffset(centerX - 20, centerY - 5);
            contentStream.showText("电子印章");
            contentStream.endText();

            contentStream.close();

            // 设置 HTTP 响应头，返回流
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=signed.pdf");
            document.save(response.getOutputStream());

            document.close();
        } catch (Exception e) {
            throw new RuntimeException("PDF 处理失败", e);
        }
    }

    private void drawCircle(PDPageContentStream contentStream, float cx, float cy, float r) throws IOException, IOException {
        float c = 0.551915024494f * r; // 近似控制点偏移量

        contentStream.moveTo(cx - r, cy);
        contentStream.curveTo(cx - r, cy + c, cx - c, cy + r, cx, cy + r);
        contentStream.curveTo(cx + c, cy + r, cx + r, cy + c, cx + r, cy);
        contentStream.curveTo(cx + r, cy - c, cx + c, cy - r, cx, cy - r);
        contentStream.curveTo(cx - c, cy - r, cx - r, cy - c, cx - r, cy);
        contentStream.closePath();
    }
}